<?php
namespace app\admin\model;
use think\db\Query;
use think\Model;
use think\Db;
class userModel extends Model
{
    public $table = 'user';

    public function userList($where = [], $field = '*', $order = 'id DESC', $limit = '0,10')
    {
        $list = Db::name($this->table)
            ->alias('a')
            ->where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();

    }

    /**
     * @param array $list
     * @return array
     */
    public function getParentUser($list = [])
    {
        if (!empty($list)) {
            foreach ($list AS $value) {
                $pid[] = $value['pid'];
            }
            $pid = implode(',', $pid);
            $where = "id IN($pid)";
            $pList = Db($this->table)->where($where)->select();
            foreach ($list as $key => $item) {
                foreach ($pList as $value) {
                    if ($item['pid'] == $value['id']) $list[$key]['pater'] = $value['username'];
                }
                $list[$key]['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
                $item['is_delete'] == 1 ? $list[$key]['deleteNmae'] = '正常' : $list[$key]['deleteNmae'] = '禁用';
            }
        }
        $lists = self::getTeamNum($list);
        return $lists;
    }

    /**
     * @param array $list
     */
    public function getTeamNum($list = [])
    {
        if (!empty($list)) {
            foreach ($list as $value) {
                $id[] = $value['id'];
            }
            if ($id) {
                $ids = join(',', $id);
                $where = "pid IN($ids)";
                $plist = Db($this->table)->where($where)->select();
                foreach ($list as $key => $value) {
                    $list[$key]['teamcount'] = 0;
                    foreach ($plist as $item) {
                        if ($value['id'] == $item['pid']) {
                            $list[$key]['teamcount']++;
                        }
                    }
                }
            }
        }
        return $list;
    }

    /**
     * 用户等级
     */
    public function userRole()
    {
        $list = Db::name('user_role')->where(['status' => 1])->order('sort DESC,id DESC')->field('id,name')->select();
        return $list;
    }

    /**
     * 获取一级团队
     */
    public function getNoeTeam($uid = 0)
    {
        $list = Db::name($this->table)
            ->alias('a')
            ->join('t_user_role b', 'a.role_id=b.id', 'left')
            ->where(['a.pid' => $uid])
            ->field('a.id,a.nickname,a.username,a.mobile,a.add_time,a.pid,a.is_delete,b.name')
            ->select();
        $list = self::getTeamNum($list);
        //获取上级
        $list = self::getParentUser($list);
        return $list;

    }

    /**
     * 获取二级团队
     */
    public function getTwoTeam($uid = 0)
    {
        //获取一级
        $list = Db::name($this->table)->where(['pid' => $uid])->select();

        //获取二级
        if (!empty($list)) {
            foreach ($list as $value) {
                $id[] = $value['id'];
            }
            $id = implode(',', $id);
        }


        $where = "a.pid in($id)";
        $lists = Db::name($this->table)
            ->alias('a')
            ->join('t_user_role b', 'a.role_id=b.id', 'LEFT')
            ->where($where)
            ->field('a.id,a.nickname,a.username,a.mobile,a.add_time,a.pid,a.is_delete,b.name')
            ->select();
        $lists = self::getTeamNum($lists);
        //获取上级
        $lists = self::getParentUser($lists);
        return $lists;
    }

    /**
     * 会员角色
     */
    protected function roleArr($role = []){
        $role = [
            1=>'钻石卡',
            2=>'金卡',
            3=>'银卡'
        ];
        return $role;
    }

    /**
     * @return mixed
     *首页饼图
     */
    public function roledata(){
        $list = Db::name('user')->where(['is_delete'=>1])->field('role_id')->select();
        $gong = count($list);
        $data[0]['num'] = 0;
        $data[1]['num'] = 0;
        $data[2]['num'] = 0;
        $data[3]['num'] = 0;
        $data[0]['name'] = '钻石会员';
        $data[1]['name'] = '金卡会员';
        $data[2]['name'] = '银卡会员';
        $data[3]['name'] = '其他会员';

        foreach ($list as $k=>$v){
            if($v['role_id'] == 1)$data[0]['num']++;
            if($v['role_id'] == 2)$data[1]['num']++;
            if($v['role_id'] == 3)$data[2]['num']++;
            if($v['role_id'] == '')$data[3]['num']++;
        }
        $data[0]['per']  = $data[0]['num']/$gong*100;
        $data[1]['per']  = $data[1]['num']/$gong*100;
        $data[2]['per']  = $data[2]['num']/$gong*100;
        $data[3]['per']  = $data[3]['num']/$gong*100;
        return $data;
    }

    /**
     * 近一周走势
     */
    public function moneyWeek(){

        //最近7天时间
        $data['time'][] = $time['data'] = date("Y-m-d",strtotime("-1 day"));
        $data['time'][] = $time['two'] = date("Y-m-d",strtotime("-2 day"));
        $data['time'][] = $time['three'] = date("Y-m-d",strtotime("-3 day"));
        $data['time'][] = $time['four'] = date("Y-m-d",strtotime("-4 day"));
        $data['time'][] = $time['five'] = date("Y-m-d",strtotime("-5 day"));
        $data['time'][] = $time['six'] = date("Y-m-d",strtotime("-6 day"));
        $data['time'][] = $time['seven'] = date("Y-m-d",strtotime("-7 day"));
        $money = array_map(function($time){
            return Db::query("SELECT SUM(real_price) as real_price,SUM(total_price) AS total_price ,SUM(tax_price) AS tax_price ,SUM(service_price) AS service_price  FROM t_order WHERE status=1 AND created_at > '$time 0:0:0' AND created_at < '$time 23:59:59'");
        },$time);
        foreach ($money as $value){
            $data['data'][] = round($value[0]['real_price'],2);
            $data['dataprice'][] = round($value[0]['total_price'],2);
            $data['datatax'][] = round($value[0]['tax_price'],2);
            $data['dataservice'][] = round($value[0]['service_price'],2);
        }
        return $data;
    }

}